package com.guli.coupon.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.guli.common.to.SkuInfoTo;
import com.guli.common.utils.R;
import com.guli.common.validation.AddValidation;
import com.guli.common.vo.CouponsAvailableVo;
import com.guli.common.vo.MemberRespVo;
import com.guli.coupon.entity.CouponEntity;
import com.guli.coupon.entity.CouponSpuCategoryRelationEntity;
import com.guli.coupon.entity.CouponSpuRelationEntity;
import com.guli.coupon.feign.ProductFeignService;
import com.guli.coupon.interceptor.LoginInterceptor;
import com.guli.coupon.service.CouponService;
import com.guli.coupon.service.CouponSpuCategoryRelationService;
import com.guli.coupon.service.CouponSpuRelationService;
import com.guli.coupon.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.coupon.dao.CouponHistoryDao;
import com.guli.coupon.entity.CouponHistoryEntity;
import com.guli.coupon.service.CouponHistoryService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.swing.plaf.metal.MetalMenuBarUI;


@Service("couponHistoryService")
public class CouponHistoryServiceImpl extends ServiceImpl<CouponHistoryDao, CouponHistoryEntity> implements CouponHistoryService {
    @Resource
    private ProductFeignService productFeignService;

    @Autowired
    private CouponService couponService;
    @Resource
    private CouponSpuRelationService couponSpuRelationService;
    @Resource
    private CouponSpuCategoryRelationService couponSpuCategoryRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CouponHistoryEntity> page = this.page(
                new Query<CouponHistoryEntity>().getPage(params),
                new QueryWrapper<CouponHistoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R getAvailableCoupons(List<OrderItemVo> orderItemVos) {
        //获取订单项所有skuid
        List<Long> skuIds = orderItemVos.stream().map(OrderItemVo::getSkuId).collect(Collectors.toList());
        //远程调用获取所有sku
        R skuInfos = productFeignService.skuInfos(skuIds);
        List<SkuInfoTo> skuInfosData = skuInfos.getData(new TypeReference<List<SkuInfoTo>>() {
        });
        //skuInfosData
        //spuIds
        List<Long> spuIds = skuInfosData.stream().map(SkuInfoTo::getSpuId).distinct().collect(Collectors.toList());
        //categoryIds
        List<Long> categoryIds = skuInfosData.stream().map(SkuInfoTo::getCatalogId).distinct().collect(Collectors.toList());
        //根据spuids查询所有优惠卷
        List<CouponSpuRelationEntity> SpuRelationEntityList = couponSpuRelationService.getBaseMapper()
                .selectList(new QueryWrapper<CouponSpuRelationEntity>().in("spu_id", spuIds));
        //根据categoryid查询所有优惠劵
        List<CouponSpuCategoryRelationEntity> categoryRelationEntities = couponSpuCategoryRelationService.getBaseMapper()
                .selectList(new QueryWrapper<CouponSpuCategoryRelationEntity>().in("category_id", categoryIds));
        //获取couponId
        List<Long> spuCouponIds = null;
        if (!CollectionUtils.isEmpty(SpuRelationEntityList)) {
            spuCouponIds = SpuRelationEntityList.stream().map(CouponSpuRelationEntity::getCouponId).collect(Collectors.toList());
        }
        List<Long> categoryCouponIds = null;
        if (!CollectionUtils.isEmpty(categoryRelationEntities)) {
            categoryCouponIds = categoryRelationEntities.stream().map(CouponSpuCategoryRelationEntity::getCouponId).collect(Collectors.toList());
        }
        //获取当前用户
        MemberRespVo memberRespVo = LoginInterceptor.memberRespThreadLocal.get();
        //查询可用优惠按
        QueryWrapper<CouponHistoryEntity> wrapper = new QueryWrapper<CouponHistoryEntity>()
                .eq(memberRespVo != null,"member_id", memberRespVo.getId())
                .in(spuCouponIds != null, "coupon_id", spuCouponIds)
                .or()
                .in(categoryCouponIds != null, "coupon_id", categoryCouponIds)
                // 0 代表全场通用卷
                .eq("use_status", 0);
        List<CouponHistoryEntity> couponHistoryEntities = this.baseMapper.selectList(wrapper);
        //若couponHistoryEntities为空
        if (CollectionUtils.isEmpty(couponHistoryEntities)) {
            return R.ok();
        }
        List<CouponsAvailableVo> availableVos = couponHistoryEntities.stream()
                .map(couponHistoryEntity -> {
                    CouponsAvailableVo couponsAvailableVo = new CouponsAvailableVo();
                    couponsAvailableVo.setId(couponHistoryEntity.getId());
                    //根绝couponId查询优惠卷信息
                    Long couponId = couponHistoryEntity.getCouponId();
                    CouponEntity coupon = couponService.getById(couponId);
                    couponsAvailableVo.setCouponName(coupon.getCouponName());
                    couponsAvailableVo.setCouponImg(coupon.getCouponImg());
                    couponsAvailableVo.setCouponType(coupon.getCouponType());
                    couponsAvailableVo.setUseStatus(couponHistoryEntity.getUseStatus());
                    return couponsAvailableVo;
                }).collect(Collectors.toList());
        return R.ok().put("data", availableVos);
    }
}